from django.db import models
from django.db.models import Model
import tsl_backend.users.models as users
import tsl_backend.frontend_api.models.route as routes


class Detail(Model):
    STATE_CHOICES = [
        (0, "处理中"),
        (1, "完成"),
        (2, "撤消"),
    ]

    # order = models.BigIntegerField(primary_key=True)
    order = models.CharField(max_length=20, primary_key=True)
    description = models.CharField(max_length=255, blank=True, null=True)
    due_date = models.DateTimeField(blank=True, null=True)
    quantity = models.IntegerField(blank=True, null=True)
    goods_type = models.CharField(max_length=100, blank=True, null=True)
    ring_type = models.CharField(max_length=20, blank=True, null=True)
    gold_type = models.CharField(max_length=20, blank=True, null=True)
    site = models.ForeignKey(users.Site, on_delete=models.PROTECT, null=True)
    ready_date = models.DateTimeField(null=True)
    pos = models.CharField(max_length=100, blank=True, null=True)
    design = models.CharField(max_length=20, blank=True, null=True)
    mould = models.IntegerField(null=True)
    remark = models.CharField(max_length=255, blank=True, null=True)
    effect = models.CharField(max_length=255, blank=True, null=True)
    product_mark_content = models.CharField(max_length=255, blank=True, null=True)
    product_mark_description = models.CharField(max_length=255, blank=True, null=True)
    product_mark_font = models.CharField(max_length=100, blank=True, null=True)
    # production_order_status = models.IntegerField(default=0)
    # state = models.CharField(max_length=20, default="open")
    state = models.IntegerField(default=0, choices=STATE_CHOICES)
    master = models.ForeignKey(routes.Master, on_delete=models.PROTECT)
    gold_print_date = models.DateTimeField(null=True)
    stone_print_date = models.DateTimeField(null=True)
    updated_due_date = models.DateTimeField(null=True)
    black_pic_url = models.URLField(null=True)

    total_count = models.PositiveSmallIntegerField(null=True)
    total_ct = models.DecimalField(max_digits=8, decimal_places=3, null=True)
    total_gram = models.DecimalField(max_digits=8, decimal_places=3, null=True)

    # flower_count = models.PositiveSmallIntegerField(default=0, null=True)
    flower_count = models.CharField(max_length=100, blank=True, null=True)
    ##
    stone_issue = models.CharField(max_length=20, blank=True, null=True)
    source = models.CharField(max_length=5, choices=[("4083", "內销"), ("1062", "外销")], verbose_name="內/外销", blank=True, null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    ori_fg_article = models.CharField(max_length=50, blank=True, null=True)

    def __str__(self):
        return f"{self.order}"

    def formatted_due_date(self):
        if self.due_date:
            return self.due_date.strftime("%Y.%m.%d")
        else:
            return None

    def formatted_ready_date(self):
        if self.ready_date:
            return self.ready_date.strftime("%Y.%m.%d %H:%M:%S")
        else:
            return None

    def formatted_gold_print_date(self):
        if self.gold_print_date:
            return self.gold_print_date.strftime("%Y.%m.%d %H:%M:%S")
        else:
            return None

    def formatted_stone_print_date(self):
        if self.stone_print_date:
            return self.stone_print_date.strftime("%Y.%m.%d %H:%M:%S")
        else:
            return None

    def formatted_updated_due_date(self):
        if self.updated_due_date:
            return self.updated_due_date.strftime("%Y.%m.%d %H:%M:%S")
        else:
            return None


class ProductionStandardBOM(Model):
    workorder = models.ForeignKey(Detail, on_delete=models.PROTECT)
    article = models.CharField(max_length=30)
    article_description = models.CharField(max_length=255)
    qty = models.IntegerField(null=True)
    weight = models.DecimalField(max_digits=8, decimal_places=3, null=True)
    unit = models.CharField(max_length=10, blank=True, null=True)
    stone_type = models.CharField(max_length=10, blank=True, null=True)
    is_stone = models.BooleanField()
    is_main_stone = models.BooleanField()

    class Meta:
        db_table = "production_standard_bom"

    def __str__(self):
        return f"{self.workorder.order}"


class ProductionActualBOM(Model):
    workorder = models.ForeignKey(Detail, on_delete=models.PROTECT)
    article = models.CharField(max_length=30)
    quantity = models.DecimalField(max_digits=8, decimal_places=2, null=True)
    weight = models.DecimalField(max_digits=8, decimal_places=3, null=True)
    unit = models.CharField(max_length=10, null=True, blank=True)
    seq = models.IntegerField(null=True)
    is_main_stone = models.BooleanField(default=False)
    laser_mark = models.CharField(max_length=30, null=True, blank=True)
    stone_color = models.CharField(max_length=10, null=True, blank=True)
    stone_clarity = models.CharField(max_length=10, null=True, blank=True)
    stone_cert = models.CharField(max_length=30, null=True, blank=True)

    class Meta:
        db_table = "production_actual_bom"

    def __str__(self):
        return f"{self.workorder.order}"
